package view.images;

import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.HashMap;

import javax.imageio.ImageIO;

public class SpriteCache {

	private HashMap<String, BufferedImage> sprites;

	public SpriteCache() {
		sprites = new HashMap<String, BufferedImage>();
	}

	public Object loadResource(String name) {
		URL url = null;
		try {
			url = getClass().getClassLoader().getResource("view/images/" + name);
			return ImageIO.read(url);
		} catch (Exception e) {
			//System.out.println("Nao foi possivel carregar a imagem " + name + " de " + url);
			//System.out.println("Erro: " + e.getClass().getName() + " " + e.getMessage());
			//System.exit(0);
			return null;
		}
	}

	public BufferedImage getSpriteInstance(String name) {
		BufferedImage img = (BufferedImage) sprites.get(name);
		if (img == null) {
			img =(BufferedImage) loadResource(name);
			sprites.put(name, img);
		}
		return img;
	}

	public void setSpriteInstance(String name) {
		BufferedImage img =(BufferedImage) loadResource(name);
		sprites.put(name, img);
		
	}
	
	public HashMap<String, BufferedImage> getSprites() {
		return sprites;
	}


}